package com.fbinggun.fbinggunutil.statistics;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.API.TiemUtil;
import com.fbinggun.fbinggunutil.statistics.datatype.*;
import org.bukkit.OfflinePlayer;

import java.sql.SQLException;

public class StatisticsAPI {
    StatistMain main;
    protected StatisticsAPI(){
        main=FBinggunUtil.plugin.getStatistMain();
    }

    /***
     * 获得玩家统计信息
     * @param player
     * @return
     */
    public  PlayerStatistics getPlayerStatistics(OfflinePlayer player){
        if(player==null){
            return null;
        }
        try {
            JSONObject json= main.getStatisticsTable().getJSONObject(player.getName());
            if(json==null){
                return new PlayerStatistics(player.getName());
            }else {
                return new PlayerStatistics(json);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private void create(String id,StatisticsInfo info){
        main.getProject().createInfo(id, info);
    }

    /**
     * 创建统计信息
     * @param id 统计ID
     * @param type 统计类型 LONG INT DOUBLE STRING BOOLEAN
     * @param value
     */
    public boolean create(String id,String type,String value){
        StatisticsInfo.InfoType infoType=StatisticsInfo.InfoType.valueOf(type.toUpperCase());
        return create(id,infoType,value);
    }

    public StatisticsInfo getDefaultInfo(String id){
        return main.getProject().getStatisticsInfo(id);
    }

    public boolean create(String id,StatisticsInfo.InfoType infoType,String value){
        StatisticsInfo info = null;
        if (infoType== StatisticsInfo.InfoType.LONG){
            long v= Long.parseLong(value);
            info = new LongInfo(id,v);

        }
        if (infoType== StatisticsInfo.InfoType.DOUBLE){
            double v= Double.parseDouble(value);
            info = new DoubleInfo(id,v);

        }
        if (infoType== StatisticsInfo.InfoType.INT){
            int v= Integer.parseInt(value);
            info = new IntInfo(id,v);
        }
        if (infoType== StatisticsInfo.InfoType.STRING){
            info = new StringInfo(id,value);
        }
        if (infoType== StatisticsInfo.InfoType.BOOLEAN){
            info = new BooleanInfo(id,Boolean.parseBoolean(value));
        }
        if(info!=null){
            create(id, info);
            return true;
        }

        return false;
    }


    /**
     * 获得此id统计信息的值
     * @param player
     * @param id
     * @return
     */
    public  String getPlaceholder(OfflinePlayer player,String id){
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
       return playerStatistics.getInfoV(id);
    }

    /**
     * 设置统计信息值
     * @param player
     * @param id
     * @param t
     * @param <T>
     */
    public  <T> void setValue(OfflinePlayer player,String id,T t){
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        playerStatistics.setStatisticsInfo(id,t);
    }

    public  <T> void setSValue(OfflinePlayer player,String id,String t){
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        playerStatistics.setSStatisticsInfo(id,t);
    }

    public  <T> void addValue(OfflinePlayer player,String id,T t){
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        playerStatistics.addStatisticsInfo(id,t);
    }
    public void addSValue(OfflinePlayer player,String id,String t){
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        playerStatistics.addSStatisticsInfo(id,t);
    }

    public  Long getClockTime(OfflinePlayer player,String id) {
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        return playerStatistics.getClockTime(id);
    }
    public  Long getFirstClock(OfflinePlayer player,String id) {
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
        return playerStatistics.getFirstClock(id);
    }
    public  String getClockTimeStr(OfflinePlayer player,String id) {
        if(getClockTime(player,id)==0){
            return "";
        }
        try {
            return TiemUtil.stampToTimeMillisecond(getClockTime(player,id),"MM月dd日 HH:mm");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public    String getFirstClockStr(OfflinePlayer player,String id) {
        if(getFirstClock(player,id)==0){
            return "";
        }
        try {
            return TiemUtil.stampToTimeMillisecond(getFirstClock(player,id),"MM月dd日 HH:mm");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public  void setClockTime(OfflinePlayer player,String id,Long clockTime) {
        PlayerStatistics playerStatistics = getPlayerStatistics(player);
       playerStatistics.setClockTime(id,clockTime);
    }


}
